/**
 *  Copyright 2009-2010 Steven Ottenhoff (Dual-IT)
 * 
 *  This file is part of Clazzified.
 *
 *  Clazzified is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Clazzified is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Clazzified.  If not, see http://www.gnu.org/licenses/gpl.
 * 
 */

package nl.dualit.clazzified.core.model.clazz;

import java.util.Map;

import nl.dualit.clazzified.core.model.AbstractBuilder;
import nl.dualit.clazzified.core.model.attribute.AttributeBuilder;
import nl.dualit.clazzified.store.AttributeData;
import nl.dualit.clazzified.store.ClazzData;
import nl.dualit.clazzified.store.RelationData;
import nl.dualit.clazzified.store.clazz.ClazzFields;


public class ClazzBuilder extends AbstractBuilder {
	
	private AttributeBuilder attributeBuilder;
	
	public ClazzBuilder(){
		
		attributeBuilder = new AttributeBuilder();
	}
	
	/**
	 * Builds an instance of Clazz with all subparts.
	 * 
	 * @param clazzData
	 * 
	 * @return a new instance
	 */
	public Clazz getInstance( ClazzData clazzData  ) throws NullPointerException {
		
		if( clazzData == null )
			throw new NullPointerException( "ClazzData is null" );
		
		Map<ClazzFields, String> clazzMap = clazzData.getClazzData();
		
		Clazz clazz = new Clazz( ( ( clazzMap.get( ClazzFields.CLAZZ_ID) != null ) ? Long.valueOf( clazzMap.get( ClazzFields.CLAZZ_ID) ) : null ), 
				clazzMap.get( ClazzFields.CLAZZ_NAME ),
				clazzMap.get( ClazzFields.CLAZZ_DESCRIPTION), null );
		
		// Get attribute instances
		for( AttributeData attributeData : clazzData.getAttributeData() ){
			
			clazz.addAttribute( attributeBuilder.getInstance( clazz, attributeData ) );
		}
		
		// Get relation instances
		for( RelationData relationData : clazzData.getRelationData() ){
			
		}
		
		// Get manipulation instances
		
		return clazz;
	}
	
	/**
	 * Builds a new clazz instance without subparts.
	 * 
	 * @param name
	 * @param description
	 * @param parent
	 * @return
	 */
	public Clazz getInstance( String name, String description, Clazz parent ){
		
		if( name == null )
			throw new NullPointerException( "Clazz name is null " );
		
		return new Clazz( name, description, parent );
	}

}
